<template>
  <div class="container">
    <div class="content-wrapper">
      <van-tabs v-model:active="active" class="show-tabs" scrollspy sticky>
        <template #nav-left><van-icon name="arrow-left" size="20" @click="goBack" /></template>

        <van-tab title="简介">
          <template #default>
            <div class="intro-content">
              <Intro
                :showDetail="showDetail"
                :serviceNotes="serviceNotes"
                :tourSites="tourSites"
                @changeShowId="onChangeShowId"
              />
            </div>
          </template>
        </van-tab>

        <van-tab title="详情">
          <template #default>
            <Detail :content="showDetail.content" ref="detail" />
          </template>
        </van-tab>
        <van-tab title="须知">
          <template #default>
            <Notice
              :serviceNotes="serviceNotes"
              :seatPlanURL="showDetail.seatPlanURL"
              @showFaq="onShowFaq"
            />
          </template>
        </van-tab>
        <van-tab title="推荐">
          <template #default>
            <Recommend />
          </template>
        </van-tab>

        <template #nav-right>
          <van-icon name="wap-home-o" size="20" @click="goToHome" />
        </template>
      </van-tabs>
    </div>

    <!-- 立即购买按钮 -->
    <div class="buy-btn">
      <van-button type="primary" color="#ff1d41" @click="onBuyClick"> 立即购买 </van-button>
    </div>

    <!-- 弹出FAQ -->
    <Faq :faq="clickedFaq" />

    <!-- 购买参数页面 -->
    <Purchase ref="purchase" />
  </div>
</template>

<script setup>
import Intro from '@/component/showDetail/Intro.vue'
import Detail from '@/component/showDetail/Detail.vue'
import Notice from '@/component/showDetail/Notice.vue'
import Recommend from '@/component/showDetail/Recommend.vue'
import Faq from '@/component/showDetail/Faq.vue'
import Purchase from '@/component/showDetail/Purchase.vue'

import { fetchTourSiteDetail, fetchTourSites, fetchTourSiteService } from '@/api/index.js'

import { ref, computed, onMounted, reactive, useTemplateRef, watch } from 'vue'
const active = ref(0)

const detailRef = useTemplateRef('detail')
const purchaseRef = useTemplateRef('purchase')

import { useRoute } from 'vue-router'
const route = useRoute()

const showId = computed(() => route.query.showId)

onMounted(() => {
  // getTourSiteDetail(showId.value, store.locationCity.locationCityOID)
  // getTourSites(showId.value)
  // getTourSiteService(showId.value)
})

import { useStore } from '@/stores/index.js'
import router from '@/router'
const store = useStore()

const showDetail = reactive({
  siteName: '',
  showOID: '',
  showName: '',
  recommendTagNames: [],
  posterURL: '',
  venueName: '',
  venueAddress: '',
  minPrice: '',
  firstShowTime: '',
  lastShowTime: '',
  latestShowTime: '',
  showDate: '',
  discount: '',
  content: '',
  seatPlanURL: '',
})
const tourSites = ref([]) //演出分站列表
const serviceNotes = ref([]) //演出服务事项和观演须知

// 演出站点详情
const getTourSiteDetail = async (showId, locationCityOID) => {
  console.log('获取演出详情')
  try {
    const {
      statusCode,
      result: { data },
    } = await fetchTourSiteDetail(showId, locationCityOID)

    if (statusCode === 200) {
      showDetail.siteName = data.siteName
      showDetail.showOID = data.showOID
      showDetail.showName = data.showName
      showDetail.recommendTagNames = data.recommendTagNames
      showDetail.posterURL = data.posterURL
      showDetail.venueName = data.venueName
      showDetail.venueAddress = data.venueAddress
      showDetail.minPrice = data.minPrice
      showDetail.firstShowTime = data.firstShowTime
      showDetail.lastShowTime = data.lastShowTime
      showDetail.latestShowTime = data.latestShowTime
      showDetail.showDate = data.showDate
      showDetail.discount = data.discount
      showDetail.content = data.content //演出详情
      showDetail.seatPlanURL = data.seatPlanURL //座位图
      detailRef.value.refreshContentHeight()
    }
  } catch (error) {
    console.log('获取演出详情失败', error)
  }
}

//演出分站列表
const getTourSites = async (showId) => {
  console.log('获取演出站点列表')
  try {
    const { statusCode, data } = await fetchTourSites(showId)
    if (statusCode === 200) {
      tourSites.value = data
    }
  } catch (error) {
    console.log('获取演出站点列表失败', error)
  }
}

//演出服务事项和观演须知
const getTourSiteService = async (showOID) => {
  console.log('获取演出服务事项和观演须知')
  try {
    const { statusCode, data } = await fetchTourSiteService(showOID)

    if (statusCode === 200) {
      serviceNotes.value = data
      // console.log('serviceNotes', serviceNotes.value)
    }
  } catch (error) {
    console.log('获取演出服务事项和观演须知失败', error)
  }
}

watch(
  () => route.query.showId,
  async (newShowId, oldShowId) => {
    if (newShowId && oldShowId !== newShowId) {
      await getTourSiteDetail(newShowId, store.locationCity.locationCityOID)
      await getTourSites(newShowId)
      await getTourSiteService(newShowId)
    }
  },
  { immediate: true },
)

const clickedFaq = ref(null)
const onShowFaq = (faq) => {
  console.log('onShowFaq', faq)
  clickedFaq.value = faq
}

const goToHome = () => {
  router.push({
    path: '/',
  })
}

const goBack = () => {
  router.back()
}

const onBuyClick = () => {
  // console.log('点击立即购买')

  // 弹出购买参数页面
  if (purchaseRef.value) {
    purchaseRef.value.openPopup()
  }
}

//更换了showId
const onChangeShowId = (showId) => {
  // console.log('Changing showId to:', showId)
  router.push({
    path: '/show-detail',
    query: {
      showId,
    },
  })
}
</script>

<style scoped lang="scss">
.container {
  position: relative;
  z-index: 1; // 添加这行，确保内容在遮罩层之上
  // overflow: hidden;

  .show-tabs {
    :deep(.van-tabs__nav) {
      align-items: center;
    }

    .intro-content {
      overflow: hidden;
      background-color: #fff;
      position: relative;
      z-index: 1;
      margin-top: 5px;
      /* 补偿负边距带来的内边距缺失 */
    }
  }

  // 添加这个类来为内容增加底部间距
  .content-wrapper {
    padding-bottom: 60px; // 为底部固定按钮留出空间
  }

  .buy-btn {
    z-index: 2;
    background-color: #fff;
    position: fixed;
    bottom: 0;
    left: 0;
    padding: 5px 15px;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    box-sizing: border-box;

    .van-button {
      width: 100%;
      height: 44px;
      font-size: 16px;
      font-weight: 600;
      border-radius: 22px;
    }
  }
}
</style>
